<?php

class Database
{

    private $dbConfig = Array
    (
        'dbtype' => "MYSQL",
        'host' => "localhost",
        'database' => "xml_to_mysql",
        'user' => "root",
        'password' => ""
    );

    private static $database;
    private $connection;

    private function __construct()
    {
        $this->connectToDatabase();
    }

    private function connectToDatabase()
    {
        // Create Database connection
        $this->connection = mysqli_connect($this->dbConfig["host"], $this->dbConfig["user"], $this->dbConfig["password"], $this->dbConfig["database"]);

        // Check connection
        if (mysqli_connect_errno()) {
            die("Failed to connect to MySQL: " . mysqli_connect_error());
        }
    }

    public static function getInstance()
    {
        if (!isset(self::$database)) {
            self::$database = new Database();
        }
        return self::$database;
    }

    public function executeQuery($query)
    {
        return $this->executeQueryWithConnection($this->connection, $query);
    }

    public function executeQueryWithConnection($connection, $query)
    {
        $result = mysqli_query($connection, $query);
        if (!$result) {
            die('Error: ' . mysqli_error($connection));
        }
        return $result;
    }

    public function getConnection()
    {
        return $this->connection;
    }
}